jQuery(function(event) {
	
	$('#locationsList input[type="button"].butChange').click(function(e){
		var td = $(this).parents('tr').children();
		var tdt = td.eq(2);
		var tdb = td.eq(3);
		
		var val = tdt.text();
		tdt.html('');
		
		var inp = $('<input type="text" />');
		inp.val(val);
		inp.attr('title', val);
		tdt.append(inp);
		
		var butEdit = $(this);
		butEdit.hide();

		var funCloseEditing = function(e){
			tdb.find('input[type="button"]').not('.butChange').remove();
			butEdit.show();
		};
		
		$('<input type="button" value="ok" />').click(function(e){
			var newVal = inp.val().replace(/^[\s\t\r\n]+/g, '').replace(/[\s\t\r\n]+$/g, '');
			if(newVal.length > 0)
			{
				$.ajax({
					url: '/a_admin/location/translation.saveCountryTranslation',
					method: 'post',
					timeout: 20000,
					async: true,
					dataType: 'json',
					data: {
						name: td.eq(1).text(),
						translation: newVal
					},
					success: function(data){
						if(data != undefined && data.status == 'success')
						{
							inp.remove();
							tdt.text(newVal);
							tdt.removeAttr('style');
							funCloseEditing();
						}
						else
						{
							alert('Translation saving error!');
						}							
					},
					error: function(){
						alert('Translation saving error!');
					}
				});
			}
			else
			{
				alert('Please define a translation before saving the country.');
				inp.focus();
			}
		}).appendTo(tdb);
		
		$('<input type="button" value="cancel" />').click(function(){
			var val = inp.attr('title');
			inp.remove();
			tdt.text(val);
			funCloseEditing();
		}).appendTo(tdb);
		
	})
	;

	
});
